package fr.sii.android.accessibility;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.example.webservcicetest.R;

import fr.sii.android.accessibility.model.MapEntry;

/**
 * A fragment representing a single Item detail screen. This fragment is either
 * contained in a {@link ItemListActivity} in two-pane mode (on tablets) or a
 * {@link ItemDetailActivity} on handsets.
 */
public class ItemDetailFragment extends Fragment {
	/**
	 * The fragment argument representing the name of the object that this
	 * fragment represents.
	 */
	public static final String ARG_NAME = "name";
	/**
	 * The fragment argument representing the latitude of the object that this
	 * fragment represents.
	 */
	public static final String ARG_LATITUDE = "lat";
	/**
	 * The fragment argument representing the longitude of the object that this
	 * fragment represents.
	 */
	public static final String ARG_LONGITUDE = "lon";
	/**
	 * The fragment argument representing the URL of the object that this
	 * fragment represents.
	 */
	public static final String ARG_URL = "url";

	/**
	 * The map entry this fragment is presenting.
	 */
	private MapEntry mItem;

	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public ItemDetailFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle arguments = getArguments();
		if (arguments.containsKey(ARG_NAME)
				&& arguments.containsKey(ARG_LATITUDE)
				&& arguments.containsKey(ARG_LONGITUDE)
				&& arguments.containsKey(ARG_URL)) {
			mItem = new MapEntry();
			mItem.name = arguments.getString(ARG_NAME);
			mItem.lat = arguments.getString(ARG_LATITUDE);
			mItem.lon = arguments.getString(ARG_LONGITUDE);
			mItem.url = arguments.getString(ARG_URL);

		}
	}

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		// root view : fragment handle itself its own view (it is not managed by the main activity)
		View rootView = inflater.inflate(R.layout.fragment_item_detail,
				container, false);

		// Show the dummy content as text in a TextView.
		if (mItem != null) {
			((TextView) rootView.findViewById(R.id.item_name))
					.setText(mItem.name);
			((TextView) rootView.findViewById(R.id.item_latitude))
					.setText(mItem.lat);
			((TextView) rootView.findViewById(R.id.item_longitude))
					.setText(mItem.lon);

		}

		// DISPLAY SVG FILE
		
		WebView mainWebView = (WebView) rootView.findViewById(R.id.mainWebView);

		WebSettings webSettings = mainWebView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setBuiltInZoomControls(true);
		webSettings.setSupportZoom(true);

		mainWebView.setWebViewClient(new MyCustomWebViewClient());
		mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		
		//Manage progress bar
		mainWebView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				//still loading : progress bar is visible
				if (progress < 100) {
					getActivity().setProgressBarVisibility(true); //progress bar
					getActivity().setProgressBarIndeterminateVisibility(true); //circle showing it is loading
				}
				getActivity().setProgress(progress * 100); //update progress bar level. (%progress * 100)
				//load finished : hide progress bar
				if (progress == 100) {
					getActivity().setProgressBarVisibility(false);
					getActivity().setProgressBarIndeterminateVisibility(false);
				}
			}
		});
		
		///////////////plus tard l'url sera à récupérer dans le web service////////////////////////
		//Load svg file 
		//mainWebView.loadUrl("https://upload.wikimedia.org/wikipedia/commons/1/17/Gardens_OSM_map.svg");
		mainWebView.loadUrl(mItem.url);

		return rootView;
	}
	
	//THE WEB VIEW CLIENT
	private class MyCustomWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// view.loadUrl(url); //necessary only if we want to check url before using it otherwise skip this step (because it is unless it wastes time)
			return true;
		}
	}
}
